package com.example.demo.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.example.demo.bean.StudentEntity;
import com.example.demo.service.ExcelImportService;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: SpringAOP
 * @ClassName ExcelImportController
 * @description:
 * @author: zsn
 * @create: 2024-10-09 17:24
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/excel")
public class ExcelImportController {

    @Autowired
    private ExcelImportService excelImportService;

    @PostMapping("/import")
    public String importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<StudentEntity> list = excelImportService.importExcel(file);
            return JSONUtil.toJsonStr(list);
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常，例如返回错误信息
            throw new RuntimeException("Excel文件导入失败");
        }
    }

}